//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Interfaces;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;
using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
using Microsoft.Practices.Unity;
using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    public class RegistryManager : IModule
    {
        /// <summary>
        /// The unity container.
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// The main control.
        /// </summary>
        private IRegionManager mainContentControl;

        /// <summary>
        /// The content presenter.
        /// </summary>
        private RegistryManagerContentPresenter contentPresenter;

        /// <summary>
        /// The left pane presenter.
        /// </summary>
        private RegistryManagerLeftPanePresenter registryLeftPanePresenter;

        /// <summary>
        /// Initializes a new instance of the <see cref="RegistryManagerModule"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="mainWindow">The main window.</param>
        public RegistryManager(IUnityContainer container, IRegionManager mainWindow)
        {
            this.container = container;
            this.mainContentControl = mainWindow;
        }

        #region IModule Members

        /// <summary>
        /// Initializes the module
        /// </summary>
        public void Initialize()
        {
            this.contentPresenter = this.container.Resolve<RegistryManagerContentPresenter>();
            this.mainContentControl.Add(this.contentPresenter.View, "workflowManagerTablehost");

            this.registryLeftPanePresenter = this.container.Resolve<RegistryManagerLeftPanePresenter>();

            ILeftPaneNavigator leftPanePresenter = this.container.Resolve<ILeftPaneNavigator>();
            leftPanePresenter.Add(ManagementStudioControlsConstants.REGISTRYMANAGERSTRING, this.registryLeftPanePresenter);
            this.InitializeItems();
        }

        #endregion

        /// <summary>
        /// Initializes the items.
        /// </summary>
        public void InitializeItems()
        {
            RegistryManagerLeftPaneItemModel workflowItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("Workflows"), "Workflows", true);
            this.registryLeftPanePresenter.Add(workflowItem, this.container.Resolve<WorkflowLeftPanePresenter>());

            RegistryManagerLeftPaneItemModel activityItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("Activities"), "Activities", false);
            this.registryLeftPanePresenter.Add(activityItem, this.container.Resolve<ActivityLeftPanePresenter>());

            if (TridentAuthentication.LoggedUserRole == UserRole.Admin)
            {
                RegistryManagerLeftPaneItemModel nodeItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("NodeLeftPaneString"), "nodes", false);
                this.registryLeftPanePresenter.Add(nodeItem, this.container.Resolve<NodeLeftPanePresenter>());
            }

            RegistryManagerLeftPaneItemModel typeInitializerItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("TypeInitializerLeftPaneString"), "typeInitializers", false);
            this.registryLeftPanePresenter.Add(typeInitializerItem, this.container.Resolve<TypeInitializerLeftPanePresenter>());

            RegistryManagerLeftPaneItemModel assemblyPackageItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("AssembliesLeftPaneTitle"), "assemblyPackage", false);
            this.registryLeftPanePresenter.Add(assemblyPackageItem, this.container.Resolve<AssemblyCatalogueLeftPanePresenter>());

            RegistryManagerLeftPaneItemModel dataReferenceProviderItem = new RegistryManagerLeftPaneItemModel(ManagementStudioResourceManager.GetString("DataReferenceProviderLeftPaneTitle"), "dataReferenceProvider", false);
            this.registryLeftPanePresenter.Add(dataReferenceProviderItem, this.container.Resolve<DataReferenceProviderLeftPanePresenter>());

            RegistryManagerPresenterFactory.RegisterPresenter(typeof(CategoryViewModel), this.container.Resolve<CategoryEditorPresenter>());
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(WorkflowCatalogViewModel), this.container.Resolve<WorkflowCenterPanePresenter>());
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(ActivityViewModel), this.container.Resolve<ActivityCenterPanePresenter>());
 
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(TypeInitializerModel), this.container.Resolve<TypeInitializerCenterPanePresenter>());
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(AssemblyPackageViewModel), this.container.Resolve<AssemblyCatalogueCenterPanePresenter>());
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(DataReferenceProviderModel), this.container.Resolve<DataReferenceProviderCenterPanePresenter>());
            RegistryManagerPresenterFactory.RegisterPresenter(typeof(DataReferenceStoreModel), this.container.Resolve<DataReferenceStoreCenterPanePresenter>());

            if (TridentAuthentication.LoggedUserRole == UserRole.Admin)
            {
                RegistryManagerLeftPaneItemModel userItem = new RegistryManagerLeftPaneItemModel("Users", "Users", false);
                this.registryLeftPanePresenter.Add(userItem, this.container.Resolve<UserLeftPanePresenter>());

                RegistryManagerLeftPaneItemModel groupItem = new RegistryManagerLeftPaneItemModel("Groups", "Groups", false);
                this.registryLeftPanePresenter.Add(groupItem, this.container.Resolve<GroupLeftPanePresenter>());

                RegistryManagerPresenterFactory.RegisterPresenter(typeof(UserViewModel), this.container.Resolve<UserCenterPanePresenter>());
                RegistryManagerPresenterFactory.RegisterPresenter(typeof(GroupViewModel), this.container.Resolve<GroupCenterPanePresenter>());
                RegistryManagerPresenterFactory.RegisterPresenter(typeof(NodeViewModel), this.container.Resolve<NodeCenterPanePresenter>());
            }

        }
    }
}
